package com.yuanyefu.aop;

import org.springframework.aop.MethodBeforeAdvice;
import org.springframework.cglib.proxy.InvocationHandler;
import org.springframework.cglib.proxy.Proxy;

import java.lang.reflect.Method;

/**
 * Created by Administrator on 2017/9/17 0017.
 */
public class AopProxyFactory {
    public static Object createProxy(final Object target, final MethodBeforeAdvice methodBeforeAdvice){
        return Proxy.newProxyInstance(
                target.getClass().getClassLoader(),
                target.getClass().getInterfaces(), //代理类实现接口
                new InvocationHandler() { //指定拦截器
            public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                methodBeforeAdvice.before(method,args,target);
                return method.invoke(target,args);
            }
        });
    }
}
